package com.lyj.ssm.controller;

import com.lyj.ssm.entity.User;
import com.lyj.ssm.service.ITestService;
import com.lyj.ssm.service.SayService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.RedisUtil;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by yujin.liu on 2018/7/11.
 */
@Controller
@RequestMapping("/api/test")
public class TestController {

    @Autowired
    private ITestService testService;

    @Resource
    @Qualifier("sayService")
    private SayService sayService;


    @ApiOperation("测试接口")
    @RequestMapping(value = "/helloWorld" , method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String helloWorld(@ApiParam(value = "输入字符串") @RequestParam(value = "inputStr") String inputStr){
        return testService.helloWorld(inputStr);
    }

    @ApiOperation("获取用户")
    @RequestMapping(value = "/getUser" , method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public User getUser(@ApiParam(value = "id") @RequestParam(value = "id") Long id){
        return testService.getUser(id);
    }


    @RequestMapping(value = "/redis", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String redisTest(@RequestParam(value = "value") String value) {

        try {
            RedisUtil.setString("123",value);//向redis里存字符串 key-value
            System.out.println( RedisUtil.getString("123"));//从radis里取数据 key
            return RedisUtil.getString("123");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    @RequestMapping(value = "/dubboClient", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public void dubboClient(@RequestParam(value = "value") String value) {

/*        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("xml/dubbo-client.xml" );
        context.start();

        SayService sayService = (SayService) context.getBean("sayService");*/
        sayService.say(value);
    }

}
